/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaSalidaHeladera.java
 *
 * Created on 22/04/2010, 11:19:20
 */

package abstraccionhardware;

import shell.Kernel;
import dominio.Ingrediente;
import dominio.Receta;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;


/**
 *
 * @author Marcelo
 */
public class VentanaSalidaHeladera extends javax.swing.JFrame
{

    private Collection recetas = new ArrayList();
    private Collection ingredientes = new ArrayList();
    private Kernel kernel;

    /** Creates new form VentanaSalidaHeladera */
    public VentanaSalidaHeladera()
    {
//        try
//        {
//           UIManager.setLookAndFeel("net.sourceforge.napkinlaf.NapkinLookAndFeel");
//        }
//        catch (Exception e)
//        {
//            e.printStackTrace();
//        }
        initComponents();
        this.inicializar();
    }

    /** Creates new form VentanaSalidaHeladera */
    public VentanaSalidaHeladera(Kernel kernel)
    {
//        try
//        {
//           UIManager.setLookAndFeel("net.sourceforge.napkinlaf.NapkinLookAndFeel");
//        }
//        catch (Exception e)
//        {
//            e.printStackTrace();
//        }
        initComponents();
        this.inicializar();
        this.kernel = kernel;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableRecetas = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextAreaInstrucciones = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableIngredientes = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextAreaNombre = new javax.swing.JTextArea();
        jLabelCantidad1 = new javax.swing.JLabel();
        jTextFieldCalorias = new javax.swing.JTextField();
        jTextFieldPorciones = new javax.swing.JTextField();
        jLabelCantidad2 = new javax.swing.JLabel();
        jTextFieldTiempoPreparado = new javax.swing.JTextField();
        jLabelCantidad3 = new javax.swing.JLabel();
        jLabelCantidad4 = new javax.swing.JLabel();
        jTextFieldCategoria = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Recetas");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Recetas"));

        jTableRecetas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableRecetas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableRecetasMouseClicked(evt);
            }
        });
        jTableRecetas.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTableRecetasKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTableRecetas);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Procedimiento"));

        jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        jTextAreaInstrucciones.setColumns(20);
        jTextAreaInstrucciones.setLineWrap(true);
        jTextAreaInstrucciones.setRows(5);
        jTextAreaInstrucciones.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextAreaInstrucciones.setEnabled(false);
        jScrollPane3.setViewportView(jTextAreaInstrucciones);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Ingredientes"));

        jTableIngredientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", "Cantidad", "Unidad"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableIngredientes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableIngredientesMouseClicked(evt);
            }
        });
        jTableIngredientes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTableIngredientesKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(jTableIngredientes);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Informacion de Receta"));

        jLabel3.setBackground(new java.awt.Color(255, 255, 204));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Nombre de la Receta");
        jLabel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel3.setOpaque(true);

        jTextAreaNombre.setColumns(20);
        jTextAreaNombre.setRows(5);
        jTextAreaNombre.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextAreaNombre.setEnabled(false);
        jScrollPane4.setViewportView(jTextAreaNombre);

        jLabelCantidad1.setBackground(new java.awt.Color(255, 255, 204));
        jLabelCantidad1.setText("Calorias");
        jLabelCantidad1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelCantidad1.setOpaque(true);

        jTextFieldCalorias.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextFieldCalorias.setEnabled(false);

        jTextFieldPorciones.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextFieldPorciones.setEnabled(false);
        jTextFieldPorciones.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldPorcionesMouseClicked(evt);
            }
        });

        jLabelCantidad2.setBackground(new java.awt.Color(255, 255, 204));
        jLabelCantidad2.setText("Porciones");
        jLabelCantidad2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelCantidad2.setOpaque(true);

        jTextFieldTiempoPreparado.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextFieldTiempoPreparado.setEnabled(false);

        jLabelCantidad3.setBackground(new java.awt.Color(255, 255, 204));
        jLabelCantidad3.setText("Tiempo de Preparado");
        jLabelCantidad3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelCantidad3.setOpaque(true);

        jLabelCantidad4.setBackground(new java.awt.Color(255, 255, 204));
        jLabelCantidad4.setText("Categoria");
        jLabelCantidad4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabelCantidad4.setOpaque(true);

        jTextFieldCategoria.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextFieldCategoria.setEnabled(false);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabelCantidad3, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldTiempoPreparado, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabelCantidad1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldCalorias, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25)
                .addComponent(jLabelCantidad2, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldPorciones, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabelCantidad4, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldCategoria, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
                .addGap(64, 64, 64))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCantidad1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldCalorias, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldPorciones, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelCantidad2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCantidad3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldTiempoPreparado, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCantidad4, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTableRecetasMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_jTableRecetasMouseClicked
    {//GEN-HEADEREND:event_jTableRecetasMouseClicked
        // TODO add your handling code here:
        this.mostrarIngredientes();
}//GEN-LAST:event_jTableRecetasMouseClicked

    private void jTableRecetasKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_jTableRecetasKeyPressed
    {//GEN-HEADEREND:event_jTableRecetasKeyPressed

}//GEN-LAST:event_jTableRecetasKeyPressed

    private void jTableIngredientesMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_jTableIngredientesMouseClicked
    {//GEN-HEADEREND:event_jTableIngredientesMouseClicked
        // TODO add your handling code here:
////        this.mostrarIngrediente();
}//GEN-LAST:event_jTableIngredientesMouseClicked

    private void jTableIngredientesKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_jTableIngredientesKeyPressed
    {//GEN-HEADEREND:event_jTableIngredientesKeyPressed
  
            
}//GEN-LAST:event_jTableIngredientesKeyPressed

    private void jTextFieldPorcionesMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_jTextFieldPorcionesMouseClicked
    {//GEN-HEADEREND:event_jTextFieldPorcionesMouseClicked
//        this.ajustarTamanioColumna();
    }//GEN-LAST:event_jTextFieldPorcionesMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaSalidaHeladera().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelCantidad1;
    private javax.swing.JLabel jLabelCantidad2;
    private javax.swing.JLabel jLabelCantidad3;
    private javax.swing.JLabel jLabelCantidad4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTableIngredientes;
    private javax.swing.JTable jTableRecetas;
    private javax.swing.JTextArea jTextAreaInstrucciones;
    private javax.swing.JTextArea jTextAreaNombre;
    private javax.swing.JTextField jTextFieldCalorias;
    private javax.swing.JTextField jTextFieldCategoria;
    private javax.swing.JTextField jTextFieldPorciones;
    private javax.swing.JTextField jTextFieldTiempoPreparado;
    // End of variables declaration//GEN-END:variables

    private void inicializar()
    {
        this.centrar();
        this.ajustarTamanioColumna();
    }

    private void centrar()
    {
        Dimension pantalla, cuadro;
	pantalla = Toolkit.getDefaultToolkit().getScreenSize();
	cuadro = this.getSize();
	this.setLocation(((pantalla.width - cuadro.width)/2), (pantalla.height - cuadro.height)/2);
    }

    private void ajustarTamanioColumna()
    {
        TableColumn column = null;
        for (int i = 0; i < 4; i++)
        {
            column = jTableIngredientes.getColumnModel().getColumn(i);
            switch(i)
            {
                case 0:
                        column.setPreferredWidth(32);
////                        System.out.println("El tamaño de la clumna 1 es: "+column.getPreferredWidth());
                        break;
                case 1:
                        column.setPreferredWidth(178);
//                        System.out.println("El tamaño de la clumna 2 es: "+column.getPreferredWidth());
                        break;
                case 2:
                        column.setPreferredWidth(46);
//                        System.out.println("El tamaño de la clumna 3 es: "+column.getPreferredWidth());
                        break;
                case 3:
//                        System.out.println("El tamaño de la clumna 3 es: "+column.getPreferredWidth());
                        column.setPreferredWidth(44);
                        break;
            }
        }

        for (int i = 0; i < 2; i++)
        {
            column = jTableRecetas.getColumnModel().getColumn(i);
            switch(i)
            {
                case 0:
                        column.setPreferredWidth(15);
//                        System.out.println("El tamaño de la clumna 1 es: "+column.getPreferredWidth());
                        break;
                case 1:
                        column.setPreferredWidth(254);
//                        System.out.println("El tamaño de la clumna 2 es: "+column.getPreferredWidth());
                        break;
                case 2:
//                        column.setPreferredWidth(10);
//                        System.out.println("El tamaño de la clumna 1 es: "+column.getPreferredWidth());
                        break;
            }
        }
    }

    private void mostrarIngredientes()
    {
        Receta r = this.seleccionarReceta(Integer.parseInt(String.valueOf(this.jTableRecetas.getValueAt(this.jTableRecetas.getSelectedRow(), 0))));
        this.ingredientes = r.getIngrediente();
        this.cargarIngredientes();
        this.jTextAreaInstrucciones.setText(r.getInstrucciones().trim());
        this.jTextAreaNombre.setText(r.getNombre().trim());
        this.jTextFieldCalorias.setText(String.valueOf(r.getCalorias()));
        this.jTextFieldCategoria.setText(r.getCategoria().trim());
        this.jTextFieldPorciones.setText(String.valueOf(r.getPorciones()));
        this.jTextFieldTiempoPreparado.setText(r.gettiempoPreparado().trim());
    }

    public void setRecetas(Collection recetas)
    {
        this.recetas = recetas;
        this.limpiarVentana();
        this.cargarTablaRecetas();
    }

    private void cargarTablaRecetas()
    {
        this.limpiar(jTableRecetas);
        DefaultTableModel modelo = (DefaultTableModel)this.jTableRecetas.getModel();
        String datos[] = new String[2];
        Iterator it = recetas.iterator();
        Receta r = new Receta();
        while(it.hasNext())
        {
            r = (Receta)it.next();
            datos[0] = String.valueOf(r.getCodigo()).trim();
            datos[1] = r.getNombre().trim();
            modelo.addRow(datos);
        }
        r = null;
        modelo = null;
        it = null;
    }

    private void cargarIngredientes()
    {
        this.limpiar(this.jTableIngredientes);
        Iterator it = this.ingredientes.iterator();
        DefaultTableModel modelo = (DefaultTableModel)this.jTableIngredientes.getModel();
        String datos[] = new String[4];
        Ingrediente i = new Ingrediente();
        while(it.hasNext())
        {
            i = (Ingrediente)it.next();
            datos[0] = String.valueOf(i.getCodigo());
            datos[1] = i.getElemento().getNombre().trim();
            switch(i.getSeleccion())
            {
                case 0:
                        datos[2] = String.valueOf(i.getCucharadas());
                        datos[3] = "CUCHARADA";
                        break;
                case 1:
                        datos[2] = String.valueOf(i.getTazas());
                        datos[3] = "TAZA";
                        break;
                case 2:
                        datos[2] = String.valueOf(i.getPeso());
                        datos[3] = "KG";
                        break;
                case 3:
                        datos[2] = String.valueOf(i.getUnidades());
                        datos[3] = "UNIDAD";
                        break;
            }
            modelo.addRow(datos);
        }
        modelo = null;
    }


    private Receta seleccionarReceta(int codigo)
    {
        Iterator it = this.recetas.iterator();
        Receta receta = new Receta();
        while(it.hasNext())
        {
            receta = (Receta)it.next();
            if(receta.getCodigo() == codigo)
                return receta;
            receta = null;
        }
        it = null;
        return receta;
    }


    /**
     * Metodo para limpiar los registros de la tabla.
     * Quita todas las filas de la tabla.
     */
    private void limpiar(JTable tabla)
    {
        DefaultTableModel modelo = (DefaultTableModel)tabla.getModel();
        while(tabla.getRowCount() != 0)
                modelo.removeRow(0);
        modelo = null;
    }

    private void limpiarVentana()
    {
        //limpiando ingredientes
        this.limpiar(this.jTableIngredientes);
        //limpiando procedimiento
        this.jTextAreaInstrucciones.setText("");
        //limpiando el nombre
        this.jTextAreaNombre.setText("");
        //limpiando calorias
        this.jTextFieldCalorias.setText("");
        //limpiando porciones
        this.jTextFieldPorciones.setText("");
        //limpiando tiempo de preparado
        this.jTextFieldTiempoPreparado.setText("");
        //limpiando categoria
        this.jTextFieldCategoria.setText("");
    }
}
